<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="薪级" prop="newSalaryId">
        <el-cascader v-model="queryParams.newSalaryId" :options="payScaleOptions" clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-s-claim"
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnelMatters:schedule:insertPostChangeScanInfo']"
        >调整
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe v-loading="loading" :data="salaryChangeScanInfoList"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="personSalaryChangeId"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="原先薪级" align="center" prop="oldPayLevel"/>
      <el-table-column label="修改后薪级" align="center" prop="newPayLevel"/>
      <el-table-column label="生效日期" :formatter="dateFormat" align="center" prop="salaryChangeEffectiveDate"/>
      <el-table-column label="创建日期" :formatter="dateFormat" align="center" prop="createTime"/>
      <el-table-column label="是否已执行" align="center" prop="executed">
        <template slot-scope="{row}">
          <el-tag
            type="info"
            v-show="row.executed===0"
          >
            未执行
          </el-tag>
          <el-tag
            type="success"
            v-show="row.executed===1"
          > 已执行
          </el-tag>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="选择员工" prop="selectUser" v-show="form.personSalaryChangeId==null">
          <el-select v-model="form.selectUser" filterable remote style="width: 500px" reserve-keyword
                     placeholder="输入姓名进行选择" :remote-method="remoteMethod" @change="userChange()"
                     :loading="userObj.loading"
          >
            <el-option v-for="item in userObj.approverOptions" :key="item.value" :label="item.label"
                       :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" @change="obtainSalaryInformation()" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input :disabled="true" v-model="form.userName" placeholder="输入用户id自动显示"/>
        </el-form-item>
        <el-form-item label="原先薪级" prop="oldSalaryId">
          <el-cascader :disabled="true" v-model="form.oldSalaryId" :options="payScaleOptions" placeholder="输入用户id自动显示"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="修改后薪级" prop="newSalaryId">
          <el-cascader v-model="form.newSalaryId" :options="payScaleOptions"  clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="生效日期" prop="salaryChangeEffectiveDate">
          <el-date-picker clearable
                          v-model="form.salaryChangeEffectiveDate"
                          type="datetime"
                          placeholder="请选择生效日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSalarySelectList,
  insertPostChangeScanInfo, insertSalaryChangeScanInfo, obtainEmployeeStatusInformation, obtainSalaryInformation,
  selectPostChangeScanInfo,
  selectSalaryChangeScanInfo
} from '@/api/personnelMatters/personScheduled'
import moment from 'moment'
import { getUser } from '@/api/system/user'
import { debounce } from 'lodash'
import { getApproverNameByLike } from '@/api/out/leave'

export default {
  name: 'salaryChangeScanInfo',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人事考核表格数据
      salaryChangeScanInfoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        newSalaryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户id不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' }
        ],
        newSalaryId: [
          { required: true, message: '修改后薪级不能为空', trigger: 'change' }
        ],
        salaryChangeEffectiveDate: [
          { required: true, message: '生效日期不能为空', trigger: 'change ' }
        ]
      },
      //薪级 级联选择器
      payScaleOptions: [],

      userObj: {
        approverOptions: [],
        loading: false
      }
    }
  },
  created() {
    this.getList()
    getSalarySelectList().then(response => {
      this.payScaleOptions = response.data.map(item => {
        //计算子节点value，label
        let childObj = item.children.map(child => {
          return {
            value: +`${child.payId}`,
            label: `${child.payLevel}`
          }
        })

        let parentObj = {
          value: +`${item.payId}`,
          label: `${item.payType}`,
          children: childObj
        }
        return parentObj
      })
    })

  },
  methods: {
    /** 查询职务扫描表列表 */
    getList() {
      this.loading = true
      selectSalaryChangeScanInfo(this.queryParams).then(response => {
        this.salaryChangeScanInfoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userName: null,
        oldSalaryId: null,
        newSalaryId: undefined,
        postChangeEffectiveDate: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.newSalaryId != null) {
        this.queryParams.newSalaryId = this.queryParams.newSalaryId[1]
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personAssessmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加薪级调整'
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.newSalaryId != null) {
            this.form.newSalaryId = this.form.newSalaryId[1]
            insertSalaryChangeScanInfo(this.form).then(response => {
              this.$modal.msgSuccess(response.msg)
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    obtainSalaryInformation() {
      this.form.userName = null
      this.form.oldSalaryId = null
      obtainSalaryInformation(this.form.userId).then(response => {
        if (response.data.nickName != null) {
          this.form.userName = response.data.nickName
          this.form.oldSalaryId = response.data.salaryLevel
        } else {
          this.$modal.msgError('没有id为：' + this.form.userId + '的用户，请重新输入')
        }
      })
    },

    remoteMethod: debounce(function(query) {
      if (query !== '') {
        this.userObj.loading = true
        //发送请求，根据用户姓名进行模糊查询
        getApproverNameByLike(query).then((res) => {
          this.userObj.loading = false
          this.userObj.approverOptions = res.data.map((item) => {
            return {
              value: `${item.userId}`,
              label: `账号: ${item.userName}   姓名: ${item.nickName}  部门: ${item.deptName}`
            }
          })
        })
      } else {
        this.approverOptions = []
      }
    }, 500),

    userChange() {
      this.form.userId = this.form.selectUser
      this.obtainSalaryInformation()
    },

    /* 日期转换 */
    dateFormat: function(row, column) {
      var date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>
